package com.drondea.testclient.handler.cmpp;

import cn.hutool.core.util.StrUtil;
import com.drondea.sms.message.cmpp.CmppDeliverRequestMessage;
import com.drondea.sms.message.cmpp.CmppDeliverResponseMessage;
import com.drondea.testclient.panel.CmppPanel;
import com.drondea.testclient.util.counter.CmppAtomicUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 刘彦宁
 * @version V3.0.0
 * @description 回执处理器
 * @date 2020年06月22日08:48
 **/

@Slf4j
public class CmppDeliveryRequestHandler extends SimpleChannelInboundHandler<CmppDeliverRequestMessage> {

    private static final CmppPanel PANEL = CmppPanel.getInstance();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, CmppDeliverRequestMessage msg) {

        //判断是否是回执
        boolean report = msg.isReport();
        if (report) {
            PANEL.reportCountLabel.setText(StrUtil.format("报告数量: {}", CmppAtomicUtil.REPORT_COUNT.incrementAndGet()));
            log.debug("CmppDeliverRequestMessage stat = {}", msg.getReportRequestMessage().getStat());
        } else if (msg.isMsgComplete()) {
            //是否长短信组装完毕
            log.debug("组装完毕，短信内容：{}", msg.getMsgContent());
        }

        // 发送响应
        CmppDeliverResponseMessage deliverResponseMessage = new CmppDeliverResponseMessage(msg.getHeader());
        deliverResponseMessage.setMsgId(msg.getMsgId());
        deliverResponseMessage.setResult(0);
        ctx.channel().writeAndFlush(deliverResponseMessage);
    }
}
